package com.xhu.rjsx2.controller;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.xhu.rjsx2.enums.AppHttpCodeEnum;
import com.xhu.rjsx2.pojo.Course;
import com.xhu.rjsx2.pojo.ResponseResult;
import com.xhu.rjsx2.pojo.dto.CourseDTO;
import com.xhu.rjsx2.pojo.vo.CourseVO;
import com.xhu.rjsx2.pojo.vo.LayuiPageResult;
import com.xhu.rjsx2.pojo.vo.PageVO;
import com.xhu.rjsx2.service.CourseService;
import com.xhu.rjsx2.util.BeanCopyUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

@RequestMapping("/course")
@RestController
@Api(tags = "课程（course）管理接口API",description = "Course Controller")
public class CourseController {

    @Resource
    private CourseService courseService;

    @GetMapping("/listAllCourse")
    @ApiOperation(value = "查询所有课程")
    public ResponseResult listAllCourse(){
        List<Course> courseList = courseService.list();
        List<CourseVO> courseVOList = BeanCopyUtils.copyBeanList(courseList, CourseVO.class);
        return ResponseResult.okResult(courseVOList);
    }

    @GetMapping("/listAllCoursePage")
    @ApiOperation(value = "分页查询所有课程")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页数"),
            @ApiImplicitParam(name = "limit", value = "每页条数"),
            @ApiImplicitParam(name = "courseCoach", value = "教练姓名"),
            @ApiImplicitParam(name = "courseName", value = "课程名称"),
            @ApiImplicitParam(name = "courseTime", value = "上课时长(h)")
    })
    public LayuiPageResult listAllCoursePage(Integer page,Integer limit,String courseCoach,String courseName,Integer courseTime){

        PageVO pageVO = courseService.listAllCoursePage(page,limit,courseCoach,courseName,courseTime);
        return new LayuiPageResult(LayuiPageResult.CODE_SUCCESS,LayuiPageResult.MESSAGE_SUCCESS,pageVO.getRows(), pageVO.getTotal());
    }


    @PostMapping("/updateCourseById")
    @ApiOperation(value = "通过主键更新课程信息")
    public ResponseResult updateCourseById(@RequestBody CourseDTO courseDTO){
        Course course = BeanCopyUtils.copyBeanSingle(courseDTO, Course.class);
        boolean updateSuccess = courseService.updateById(course);
        return updateSuccess ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
    }


    @PostMapping("/deleteCourseById")
    @ApiOperation(value = "通过主键删除课程信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "当前课程id")
    })
    public ResponseResult deleteCourseById(Integer id){
        boolean removeSuccess = courseService.removeById(id);
        return removeSuccess ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
    }


    @PostMapping("/insertCourse")
    @ApiOperation(value = "新增课程接口")
    public ResponseResult insertCourse(@RequestBody CourseDTO courseDTO){
        // 由系统生成自增id
        courseDTO.setId(null);
        // 课程发布时间由系统获取当前时间
        String now = DateUtil.now();
        courseDTO.setCourseDate(DateUtil.parse(now));

        Course course = BeanCopyUtils.copyBeanSingle(courseDTO, Course.class);
        boolean saveSuccess = courseService.save(course);
        return saveSuccess ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
    }

}
